const Router = require('koa-router')
const updateaddress = new Router()
//！拿到前端的数据
//数据库连接池
const pg = require('pg');
let psql = new pg.Pool({
    database: 'running',
    user: 'run',
    port: 5432,
    host: '82.157.94.115',
    password: 'z12345678',
    max: 2
})

updateaddress.post('/updateaddress', async ctx => {
    let {region, name, tel, door } = ctx.request.body;
    console.log(region);
    console.log(name);
    console.log(tel);
    console.log(door);
    let updateContentPostgresql = `update address set region = '${region}',tel = '${tel}', door = '${door}' where name = '${name}'`;
    let content = await psql.query(updateContentPostgresql);
    if (content.rowCount > 0) {
        var updateaddressObj = [];
        for (var i = 0; i < content.rowCount; i++) {
            var obj = {
                region: content.rows[i].region,
                name: content.rows[i].name,
                tel:content.rows[i].tel,
                door:content.rows[i].door
            }

            updateaddressObj.push(obj);
        }

        ctx.body = {
            updateaddressObj
        };
    }


})

module.exports = updateaddress